#include "../ResourceManagers/TextureManager.hpp"
#include "../ResourceManagers/TextureObject.hpp"
#include "../HDRDemoConstants.hpp"
#include <stdio.h>

// for getche()
#include <conio.h>

#include "SphereEntity.hpp"
#include <GL/glut.h>

// testing
#include "../Mouse/MouseManager.hpp"
#include "../Camera/CameraManager.hpp"

SphereEntity::SphereEntity() :
m_toRender( true ),
m_scale( 1.0f )
{
	TextureManager::GetInstance()->LoadTexture( CHECKER_TEX );

    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}

SphereEntity::~SphereEntity()
{
	TextureManager::GetInstance()->UnloadTexture( CHECKER_TEX );
	//getch();
	printf("kasowanie kulki\n");
}

const bool& SphereEntity::IsVisible() const
{
	return m_toRender;
}


void SphereEntity::Render()
{
	GLfloat MaterialDiffuse[4],
		    MaterialSpecular[4];

	//float   MaterialShininess[1];

	MaterialDiffuse[0] = m_diffuseMaterial.x;
	MaterialDiffuse[1] = m_diffuseMaterial.y;
	MaterialDiffuse[2] = m_diffuseMaterial.z;
	MaterialDiffuse[3] = 1.0f;
	MaterialSpecular[0] = m_specularMaterial.x;
	MaterialSpecular[1] = m_specularMaterial.y;
	MaterialSpecular[2] = m_specularMaterial.z;
	MaterialSpecular[3] = 1.0f;
	//MaterialShininess[0] = 20.0f;
	glMaterialfv( GL_FRONT, GL_DIFFUSE, MaterialDiffuse );
	glMaterialfv( GL_FRONT, GL_SPECULAR, MaterialSpecular );
	glMaterialfv( GL_FRONT, GL_SHININESS, m_shininess );

	glPushMatrix();
	//glTranslatef(-3.0f, 3.5f, -1.5f );
	glTranslatef( GetPosition().x, GetPosition().y, GetPosition().z );

	TextureObject* tex = (TextureObject*)TextureManager::GetInstance()->GetTexture( CHECKER_TEX );

	//glEnable( GL_TEXTURE_2D );
	//glBindTexture(GL_TEXTURE_2D, tex->GetTextureID() );
	//! Test triangle.
	//glBegin( GL_TRIANGLES );
	//	glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -2.0f, 2.0f, 1.0f );
	//	glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -2.0f, 0.0f, 1.0f );
	//	glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 2.0f, 2.0f, 1.0f );
	//glEnd();

	//glutSolidSphere(2.0, 25, 25);
	glutSolidTeapot( 2.0 * m_scale );
	glPopMatrix();
}

void SphereEntity::SetScale(float scale)
{
	m_scale = scale;
}

const float& SphereEntity::GetScale() const
{
	return m_scale;
}

void SphereEntity::SetDiffuseMaterial(MVector3D diff)
{
	m_diffuseMaterial = diff;
}

void SphereEntity::SetSpecularMaterial(MVector3D spec)
{
	m_specularMaterial = spec;
}

void SphereEntity::SetShininess(float shin)
{
	m_shininess[0] = shin;
}